package com.doge.walknovel.business.reader.menus

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.animation.DecelerateInterpolator
import androidx.constraintlayout.widget.ConstraintLayout
import com.doge.wnpro.databinding.LayoutReaderArgsAdjustBinding
import com.doge.walknovel.utils.dp
import com.doge.walknovel.utils.setOnClickCallback
import kotlin.math.max
import kotlin.math.min

class ReaderArgsAdjustView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null
) : ConstraintLayout(context, attrs) {
    val binding =
        LayoutReaderArgsAdjustBinding.inflate(LayoutInflater.from(context), this)

    private val maxSize = 30
    private val minSize = 8

    init {
        binding.sizeDec.setOnClickCallback {
            ReaderHelper.changeSize(max(minSize, ReaderHelper.size - 1))
            refreshSizeValue()
        }
        binding.sizeInc.setOnClickCallback {
            ReaderHelper.changeSize(min(maxSize, ReaderHelper.size + 1))
            refreshSizeValue()
        }
        binding.bgWhite.setOnClickCallback {
            ReaderHelper.changeBgColor(ReaderHelper.bgWhite)
        }
        binding.bgYellow.setOnClickCallback {
            ReaderHelper.changeBgColor(ReaderHelper.bgYellow)
        }
        binding.bgGreen.setOnClickCallback {
            ReaderHelper.changeBgColor(ReaderHelper.bgGreen)
        }
        refreshSizeValue()
    }

    private fun refreshSizeValue() {
        binding.sizeValue.text = ReaderHelper.size.toString()
    }

    fun display() {
        alpha = 0f
        translationY = 300.dp.toFloat()
        visibility = View.VISIBLE
        animate()
            .alpha(1f)
            .translationY(0f)
            .setInterpolator(DecelerateInterpolator(2f))
            .setDuration(300)
            .start()
    }
}

